<?php

$gateway_url = "https://123.56.119.177:8443/pay/pay.htm";

/**
 * 签名  生成签名串  基于sha1withRSA
 * @param string $data 签名前的字符串
 * @return string 签名串
 */
function sign($data) {
    $certs = array();
    //   $path = $_SERVER ['HTTP_HOST'].'/demo_php/cert/merchant_cert.pfx';
    $path = './cert/merchant_cert.pfx';
    openssl_pkcs12_read(file_get_contents($path), $certs, "11111111"); //其中password为你的证书密码
    if (!$certs)
        return;
    $signature = '';
    openssl_sign($data, $signature, $certs['pkey']);
    return base64_encode($signature);
}

/**
 * 验证签名： 
 * @param data：原文 
 * @param signature：签名 
 * @return bool 返回：签名结果，true为验签成功，false为验签失败 
 */
function verity($data, $signature) {
    //   $path = $_SERVER ['HTTP_HOST'].'/demo_php/cert/server_cert.cer';
    $path = './cert/server_cert.cer';
    $pubKey = file_get_contents("$path");
    $res = openssl_get_publickey($pubKey);
    $result = (bool) openssl_verify($data, base64_decode($signature), $res);
    openssl_free_key($res);
    return $result;
}

?>